package com.he.hsdyc.service;

import com.alibaba.fastjson.JSON;
import com.he.hsdyc.common.ServiceException;
import com.he.hsdyc.mapper.UserMapper;
import com.he.hsdyc.model.User;
import com.he.hsdyc.model.vo.UserVo;
import com.he.hsdyc.security.CreateHttp;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Override
    public List<User> findAll(UserVo vo) {
        return userMapper.findAll(vo.getState());
    }
//
//    @Override
//    public User2 register(String mobile, String password) {
//        //System.out.println("========================="+UserEnum.USER_EXIST.getMessage());
//        // System.out.println("========================="+UserEnum.USER_EXIST.getCode());
//
//        User2 registerUser = userMapper.findByMobile(mobile);
//        if(registerUser != null && registerUser.getState() != User2.DELETE)
//            throw new LoginExpection(LoginEnum.LOGIN_FAILED);
//        //return new Message<String>(ErrorCode.ERROR);
//        User2 user = new User2();
//        user.setMobile(mobile);
//        Date date = new Date();
//        user.setCreateTime(date);
//        String salt = new SimpleDateFormat("yyyyMMddHHmm").format(date);
//        String inPassword = DigestUtils.md5Hex( new String(password + salt));
//        user.setPassword(inPassword);
//        userMapper.insertUseGeneratedKeys(user);
//        return user;
//    }

    @Override
    public Map<?,?> login(String username, String password) throws Exception {

        Map<String, Object> userMap = userMapper.getUserByUsername(username);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

        if(!encoder.matches(password, MapUtils.getString(userMap, "password"))) {
            throw new ServiceException("用户名或密码不正确");
        }
        String httpURLConnectionPOST = CreateHttp.httpURLConnectionPOST(username, password);
        return JSON.parseObject(httpURLConnectionPOST,Map.class);
    }

    @Override
    public int insertUser(UserVo userVO) throws Exception {

        int userCount = userMapper.getUserCount(userVO.getUsername(), userVO.getRoleName());

        if(userCount > 0) {
            throw new ServiceException("用户名已存在");
        }
        // 设置图片
        userVO.setIcon("icon");
        // 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        userVO.setPassword(bCryptPasswordEncoder.encode(userVO.getPassword()));
        Date now = new Date();
        userVO.setCreateTime(now);
        userVO.setUpdateTime(now);
        userVO.setLastTime(now);
        User user = new User();
        BeanUtils.copyProperties(userVO, user);

        int insert = userMapper.insertSelective(user);
        if(insert <= 0) {
            throw new ServiceException("添加账号失败");
        }
        return insert;
    }
}
